{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Variables and Assignment\n",
    "\n",
    "**Time**\n",
    "- Teaching: 5 min\n",
    "- Challenges: 10 min\n",
    "\n",
    "**Questions**\n",
    "- \"How can I store data in programs?\"\n",
    "\n",
    "**Learning Objectives**\n",
    "- \"Write programs that [assign](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#assign) scalar values to variables and perform calculations with those values.\"\n",
    "- \"Correctly trace value changes in programs that use scalar assignment.\"\n",
    "\n",
    "* * * * *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Use variables to store values.\n",
    "\n",
    "*   Variables are names for values.\n",
    "*   In Python the `=` symbol assigns the value on the right to the name on the left.\n",
    "*   The variable is created when a value is assigned to it.\n",
    "*   Here's Python code that assigns an age to a variable `age`\n",
    "    and a name in quotation marks to a variable `first_name`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age = 42\n",
    "first_name = 'Ahmed'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*   Variable names:\n",
    "    *   cannot start with a digit\n",
    "    *   cannot contain spaces, quotation marks, or other punctuation\n",
    "    *   *may* contain an underscore (typically used to separate words in long variable names)\n",
    "*   Underscores at the start like `__alistairs_real_age` have a special meaning\n",
    "    so we won't do that until we understand the convention.\n",
    "\n",
    "## Use `print` to display values.\n",
    "\n",
    "*   Python has a built-in function called `print` that prints things as text.\n",
    "*   Call the function (i.e., tell Python to run it) by using its name.\n",
    "*   Provide values to the function (e.g., things to print) in parentheses.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(first_name, 'is', age, 'years old')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*   `print` automatically puts a single space between items to separate them.\n",
    "*   And wraps around to a new line at the end.\n",
    "\n",
    "## Variables persist between cells.\n",
    "\n",
    "*   Variables defined in one cell exist in all following cells.\n",
    "*   Notebook cells are just a way to organize a program:\n",
    "    as far as Python is concerned,\n",
    "    all of the source code is one long set of instructions.\n",
    "\n",
    "## Variables must be created before they are used.\n",
    "\n",
    "*   If a variable doesn't exist yet, or if the name has been mis-spelled,\n",
    "    Python reports an error.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(last_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*   The last line of an error message is usually the most informative.\n",
    "*   We will look at error messages in detail [later](https://github.com/dlab-berkeley/python-intensive/blob/master/Day_3/15_Errors.ipynb).\n",
    "\n",
    "## Python is case-sensitive.\n",
    "\n",
    "*   Python thinks that upper- and lower-case letters are different,\n",
    "    so `Name` and `name` are different variables.\n",
    "*   Again,\n",
    "    there are conventions around using upper-case letters at the start of variable names\n",
    "    so we will use lower-case letters for now.\n",
    "\n",
    "## Use meaningful variable names.\n",
    "\n",
    "*   Python doesn't care what you call variables as long as they obey the rules\n",
    "    (alphanumeric characters and the underscore).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "flabadab = 42\n",
    "ewr_422_yY = 'Ahmed'\n",
    "print(ewr_422_yY, 'is', flabadab, 'years old')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*   Use meaningful variable names to help other people understand what the program does.\n",
    "*   The most important \"other person\" is your future self.\n",
    "\n",
    "## Variables can be used in calculations.\n",
    "\n",
    "*   We can use variables in calculations just as if they were values.\n",
    "    *   Remember, we assigned 42 to `age` a few lines ago."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age = age + 3\n",
    "print('Age in three years:', age)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Namespace\n",
    "\n",
    "To display the scope namespace of variables we can use the `dir` function:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get a dictionary of local variables, we can call `locals`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "locals()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With both functions, we see a lot of predefined variables in the Python and Jupyter environment, but also `age`, `first_name`, `flabadab`, and `ewr_422_yY`, which we created above."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# `%who` and `%whos`\n",
    "\n",
    "If `dir()` and `locals()` are too confusing, try these [magic](https://towardsdatascience.com/top-10-magic-commands-in-python-to-boost-your-productivity-1acac061c7a9) [commands](https://ipython.readthedocs.io/en/stable/interactive/magics.html) instead. \n",
    "\n",
    "`%who` will return the variables you have saved:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%who"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`%whos` will display the variables, their type, and information about the data they contain:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%whos"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 1: Making and Printing Variables\n",
    "\n",
    "1. Make 3 variables: `name` (with your full name), `city` (where you were born) and `year` (when you were born).\n",
    "2. Print these three variables so that it prints `[your name] was born in [city] in [year]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 2: Swapping Values\n",
    "\n",
    "Draw a table showing the values of the variables in this program after each statement is executed.\n",
    "\n",
    "In simple terms, what do the last three lines of this program do?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lowest = 1.0\n",
    "highest = 3.0\n",
    "temp = lowest\n",
    "lowest = highest\n",
    "highest = temp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 3: Predicting Values\n",
    "\n",
    "What is the final value of `position` in the program below?\n",
    "\n",
    "(Try to predict the value without running the program, then check your prediction.)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "initial = \"left\"\n",
    "position = initial\n",
    "initial = \"right\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 4: Syntax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Why does the following code fail? What is it probably supposed to do, and what is it actually doing?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age == 31"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the following?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "31 = age"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Fix the above cells."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*****\n",
    "\n",
    "## Keypoints\n",
    "\n",
    "1. \"Use variables to store values.\"\n",
    "2. \"Use `print` to display values.\"\n",
    "3. \"Variables persist between cells.\"\n",
    "4. \"Variables must be created before they are used.\"\n",
    "5. \"Python is case-sensitive.\"\n",
    "6. \"Variables can be used in calculations.\"\n",
    "7. \"Use meaningful variable names.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
